﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using BTAlertWinService.Logging;
namespace BTAlertWinService
{
    public class MailComponent
    {
        public static bool SendEmail(string message, string subject, IList<string> emailList)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["EmailServer"]));

                mail.From = new MailAddress(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["MailUserName"]));
                foreach(string emaillTo in emailList)
                {
                    mail.To.Add(emaillTo);
                }                
                mail.IsBodyHtml = true;
                mail.Subject = subject;
                mail.Body = message;

                SmtpServer.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MailPort"]);
                SmtpServer.Credentials = new System.Net.NetworkCredential(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["MailUserName"]), Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["MailPassword"]));
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);                
            }
            catch (Exception ex)
            {
                FileLogger.Log(ex.ToString());
            }
            return true;
        }
    }
}
